package DAL;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;

import entities.Admin;
import entities.ConnectionManager;

public class AdminDA {
	public Admin[] selectAdmins(Admin user)
	{
		boolean whereClause = false;
		String query = "SELECT * from Admin  " ;
		if(user.getAdminID() != -1)
		{
			query += "WHERE Adminid = " + user.getAdminID();
			whereClause = true;
		}
				
		if(!user.getEmail().equals("")) 
		{
			if(whereClause)
				query += " AND email = '" + user.getEmail() + "'";
			else
				query += " WHERE email = '" + user.getEmail() + "'";
			whereClause = true;
		}
		if(!user.getUsername().equals("")) 
		{
			if(whereClause)
				query += " AND username = '" + user.getUsername() + "'";
			else
				query += " WHERE username = '" + user.getUsername() + "'";
			whereClause = true;
		}

		ConnectionManager con = new ConnectionManager();
		ResultSet rs = con.executeSelectQuery(query);
		
		ArrayList<Admin> users = new ArrayList<Admin>();

		
		try {
			while(rs.next())
			{
				Admin tempUser= new Admin();
				tempUser.setAdminID(rs.getInt("AdminID"));
				tempUser.setUsername(rs.getString("username"));
				tempUser.setEmail(rs.getString("email"));
				users.add(tempUser);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(users.size());
		System.out.println(query);
		Admin [] temp = new Admin [0];
		temp = users.toArray(temp);
		System.out.println(Arrays.toString(temp));
		return temp;

	}


	public boolean updateAdmin(Admin user)
	{
		boolean setClause = false;
		String query = "UPDATE Admin SET " ;
		if(!user.getEmail().equals("")) 
		{
			query += " email = '" + user.getEmail() + "'  ";
			setClause = true;
		}
		if(!user.getUsername().equals("")) 
		{
			if(setClause)
				query += " , username = '" + user.getUsername() + "'";
			else
				query += " username = '" + user.getUsername() + "'";
			setClause = true;
		}
		
		query += " WHERE AdminID = "+ user.getAdminID();
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return (x==1);
	}
	
	public boolean insertAdmin(Admin user)
	{
		String query = "INSERT INTO Admin (username , email) VALUES " +
				"('" +user.getUsername() + "' , '" + user.getEmail() + "')";
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x==1;
	}
	
	public int deleteAdmin(Admin user)
	{
		boolean whereClause = false;
		String query = "DELETE FROM Admin ";
		if(user.getAdminID() != -1)
		{
			query += "WHERE AdminID = " + user.getAdminID();
			whereClause = true;
		}
				
		if(!user.getEmail().equals("")) 
		{
			if(whereClause)
				query += " AND email = '" + user.getEmail() + "'";
			else
				query += " WHERE email = '" + user.getEmail() + "'";
			whereClause = true;
		}
		if(!user.getUsername().equals("")) 
		{
			if(whereClause)
				query += " AND username = '" + user.getUsername() + "'";
			else
				query += " WHERE email = '" + user.getUsername() + "'";
			whereClause = true;
		}
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x;
	}
	
	
	public static void main(String[] args) {
		Admin u = new Admin();

		
		//u.setEmail("asdsadasd");
		u.setUsername("admin121asd");
		u.setAdminID(1);
		AdminDA da = new AdminDA();
		System.out.println(da.deleteAdmin(u));
	}

}
